package com.jt.chapter03.controller;

import com.jt.chapter03.BO.UserBO;
import com.jt.chapter03.service.UserService;
import com.jt.chapter03.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jiang tao
 * @date 2022/1/6
 * @description:
 **/
@RestController
public class LoginController {
    @Autowired
    private UserService userService;
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody UserBO userBO){
        String token = userService.login(userBO.getUsername(), userBO.getPassword());
        if (token == null) {
            return Result.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return Result.success(tokenMap);
    }


    @RequestMapping("/hello")
    public Result  hello(){
        return Result.success("恭喜，成功访问hello接口");
    }
}
